package com.qf.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultEntity<T> {
    private static final String SUCCESS = "success";

    private static final String ERROR = "error";

    private String status;

    private Integer code;

    private String msg;

    private T data;

    // 响应成功
    public static ResultEntity success() {
        return new ResultEntity(SUCCESS, 200, "操作成功", null);
    }
    //子类精确度比父类高，所以先调用string的，要注释掉，不然响应的是token
//    //方法重载
//    public static ResultEntity success(String token) {
//        return new ResultEntity(SUCCESS, 200, "操作成功!", null);
//
//    }

    public static ResultEntity success(Object target) {
        if (target instanceof Boolean) {
            if ((boolean) target) {
                return success();
            } else {
                return error("数据库异常");

            }
        } else   {
            return new ResultEntity(SUCCESS, 200, "操作成功", target);
        }

    }

    // 响应失败
    public static ResultEntity error(String msg) {
        return new ResultEntity(ERROR, 500, msg, null);
    }
}
